//
//  Date+Extension.swift
//  WBDemo
//
//  Created by Jin on 08/05/2018.
//  Copyright © 2018 com.baidu.123. All rights reserved.
//

import Foundation

extension Date {
     func createDateString(createAt:Date) -> String {
        //2.处理创建时间与当前时间的时间差
        let nowDate = Date()
        let interval = Int(nowDate.timeIntervalSince(createAt))
        //2.1 1分钟之内
        if interval < 60 {
            return "刚刚"
        }
        //2.2 一小时之内
        if interval < 60 * 60 {
            return "\(interval / 60)分钟前"
        }
        //2.3 一天之内
        if interval < 60 * 60 * 24 {
            return "\(interval / (60 * 60))小时前"
        }
        //2.4 昨天
        let calendar = Calendar.current
        let fmt = DateFormatter()
        if calendar.isDateInYesterday(createAt) {
            fmt.dateFormat = "昨天 HH:mm"
            let timeStr = fmt.string(from: createAt)
            return timeStr
        }
        //2.5 一年之内
        let cmps = calendar.dateComponents([Calendar.Component.year], from: createAt, to: nowDate)
        if cmps.year! < 1 {
            fmt.dateFormat = "MM-dd HH:mm"
            let timeStr = fmt.string(from: createAt)
            return timeStr
        }
        //2.6 一年以前
        if cmps.year! >= 1 {
            fmt.dateFormat = "yyyy-MM-dd HH:mm"
            let timeStr = fmt.string(from: createAt)
            return timeStr
        }
        
        return ""
    }
}
